package com.jike.jkgame.data;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jike.jkgame.Utilis;
import com.jike.jkgame.db.ResourcesDAO;

public class Mission {
	public int id;
	public int parentId;
	public String name;
	public String descript;
	public int energy;
	public int level;
	public int completeness;
	public int reward_exp;
	public int reward_cash;
	public int random_reward_Item_count;
	public RewardItem[] random_reward_item;
	public String buttonStr;
	public int animation;
	public boolean enabled;
	public int finish_cnt;
	
	
	public Mission() {
	}
	
	public void setFinishCnt(int finish) {
	    if (finish >= completeness) {
	        enabled = false;
	        finish_cnt = completeness;
	    } else {
	        enabled = true;
            finish_cnt = finish;
	    }
	}
	
	public boolean accomplished() {
		return finish_cnt >= completeness;
	}
	
	public static boolean parsDataFile(InputStream is) {
		
		DataInputStream dis = new DataInputStream(is);
		
		ResourcesDAO dao = ResourcesDAO.get();
		byte[] buff = new byte[4096];
		try {
			
			int type = dis.readInt();
			
//			if (type != FILE_TYPE) {
//				return false;
//			}
			int myVersion = dao.getDataFileVersion(type);
			int version = dis.readInt();
			
			if (myVersion >= version) {
				return false;
			}
			
			dao.clearMissions();
			
			int citiesCnt = dis.readInt();
			City city = new City();
			Event event = new Event();
			Mission mission = new Mission();
			
			for (int i=0; i<citiesCnt; ++i) {
				
				city.id = dis.readInt();
				city.name = Utilis.getString(dis, buff);
				dao.insertCity(city);
				int eventCnt = dis.readInt();
				for (int j=0; j<eventCnt; ++j) {
					event.id = dis.readInt();
					event.name = Utilis.getString(dis, buff);
					event.parentId = city.id;
					dao.insertEvent(event);
					int missionCnt = dis.readInt();
					for (int k=0; k<missionCnt; ++k) {
						mission.id = dis.readInt();
						mission.parentId = event.id;
						mission.name = Utilis.getString(dis, buff);
						mission.descript = Utilis.getString(dis, buff);
						mission.energy = dis.readInt();
						mission.level = dis.readByte();
						mission.completeness = dis.readByte();
						mission.reward_exp = dis.readInt();
						mission.reward_cash = dis.readInt();
						mission.random_reward_Item_count = dis.readInt();
						RewardItem[] rewards = new RewardItem[mission.random_reward_Item_count];
						
						for (int m=0; m<rewards.length; ++m) {
							rewards[m] = new RewardItem();
							rewards[m].id = dis.readInt();
							rewards[m].possibility = dis.readByte();
							rewards[m].type = dis.readByte();
						}
						mission.random_reward_item = rewards;
						mission.buttonStr = Utilis.getString(dis, buff);
						mission.animation = dis.readByte();
						
						dao.insertMission(mission);
						
					}
					
				}
				
			}
			
			dao.updateDataFileVersion(type, version);
			
		} catch (IOException e) {
			return false;
		}
		
		return false;
		
	}
	
}
